package com.streambolics.std;

/**
 * A SAIN parser that invokes named methods in a given target.
 * 
 * This object implements a bean-like protocol to feed parsed SAIN data to a
 * target object. It will call various methods in the target object with a name
 * based on the section name. When handling data for a section named Xxxx, the
 * parser will call the following methods:
 * 
 * * beginXxxx () will be called at the beginning of the section. An exception
 * will be raised if the target object does not implement this method.
 * 
 * * addXxxx (String aValue) will be called for each value. If the target object
 * does not implement this method, then addXxxx (String aName, String aValue)
 * will be called instead. If this method doesn't exist either, an exception
 * will be thrown.
 * 
 * * endXxxx () will be called last. If the target does not implement this
 * method, execution continues as normal with the next section.
 * 
 * @author Sauron
 * 
 */
public class BeanSainParser extends SainParser
{
    private final Invoker _target;

    public BeanSainParser(Object aTarget)
    {
        _target = new Invoker (aTarget);
    }

    @Override
    protected void addMapping (String aSectionName, String aName, String aValue) throws Exception
    {
        _target.invoke ("add" + aSectionName, aName, aValue);
    }

    @Override
    protected void addValue (String aSectionName, String aValue) throws Exception
    {
        if (!_target.invokeOptional ("add" + aSectionName, aValue))
        {
            super.addValue (aSectionName, aValue);
        }
    }

    @Override
    protected void beginSection (String aSectionName) throws Exception
    {
        _target.invokeOptional ("begin" + aSectionName);
    }

    @Override
    protected void endSection (String aSectionName) throws Exception
    {
        _target.invokeOptional ("end" + aSectionName);
    }
}
